/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import utils.JDBCUtil;
import dao.StudentDaoJTableImpl;
import dao.StudentDao;
import entity.Student;

import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

public class MainJFrame extends javax.swing.JFrame {

	/**
	 * Creates new form MainJFrame
	 */
	public MainJFrame() {
		initComponents();

	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		aboutDialog = new javax.swing.JDialog();
		jPanel2 = new javax.swing.JPanel();
		jButton5 = new javax.swing.JButton();
		jLabel1 = new javax.swing.JLabel();
		jScrollPane1 = new javax.swing.JScrollPane();
		jTable1 = new javax.swing.JTable();
		jPanel1 = new javax.swing.JPanel();
		addButton = new javax.swing.JButton();
		deleteButton = new javax.swing.JButton();
		showButton = new javax.swing.JButton();
		searchButton = new javax.swing.JButton();
		editButton = new javax.swing.JButton();
		saveChangeButton = new javax.swing.JButton();

		jMenuBar2 = new javax.swing.JMenuBar();
		jMenu3 = new javax.swing.JMenu();
		jMenuItem3 = new javax.swing.JMenuItem();
		jMenu4 = new javax.swing.JMenu();
		jMenuItem4 = new javax.swing.JMenuItem();
		stuNoJLabel = new javax.swing.JLabel();
		stuNoJTextField = new javax.swing.JTextField();
		nameJLabel = new javax.swing.JLabel();
		nameJTextField = new javax.swing.JTextField();
		// addButton = new javax.swing.JButton();
		// deleteButton = new javax.swing.JButton();
		genderJLabel = new javax.swing.JLabel();
		genderJTextField = new javax.swing.JTextField();
		birthDateJTextField = new javax.swing.JTextField();
		birhDateJLabel = new javax.swing.JLabel();
		statusJLabel = new javax.swing.JLabel();
		statusJTextField = new javax.swing.JTextField();
		addressJLabel = new javax.swing.JLabel();
		addressJTextField = new javax.swing.JTextField();
		phoneJLabel = new javax.swing.JLabel();
		phoneJTextField = new javax.swing.JTextField();
		dormJLabel = new javax.swing.JLabel();
		dormJTextField = new javax.swing.JTextField();

		aboutDialog.setTitle("About Dialog Test");
		aboutDialog.setPreferredSize(new java.awt.Dimension(250, 250));

		jButton5.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
		jButton5.setText("OK");
		jButton5.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton5ActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jButton5, javax.swing.GroupLayout.Alignment.TRAILING,
				javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE));
		jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				jButton5, javax.swing.GroupLayout.Alignment.TRAILING,
				javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE));

		aboutDialog.getContentPane().add(jPanel2,
				java.awt.BorderLayout.PAGE_END);

		jLabel1.setFont(new java.awt.Font("宋体", 0, 24)); // NOI18N
		jLabel1.setText("<html><h1><i>Student Message Manager System</i></h1><hr>By Kzq and Gzl</html>");
		aboutDialog.getContentPane().add(jLabel1,
				java.awt.BorderLayout.PAGE_START);

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("信息管理");

		jTable1.setModel(new javax.swing.table.DefaultTableModel(
				new Object[][] {

				}, new String[] { "学号", "姓名", "性别", "出生日期", "政治面貌", "家庭住址",
						"电话", "宿舍号" }) {
			Class[] types = new Class[] { java.lang.String.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class, java.lang.String.class,
					java.lang.String.class };

			public Class getColumnClass(int columnIndex) {
				return types[columnIndex];
			}
		});
		jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				jTable1MouseClicked(evt);
			}
		});
		jScrollPane1.setViewportView(jTable1);

		getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

		addButton.setText("Add");
		addButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				addButtonActionPerformed(evt);
			}
		});

		deleteButton.setText("Delete");
		deleteButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				deleteButtonActionPerformed(evt);
			}
		});

		showButton.setText("Show");
		showButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				showButtonActionPerformed(evt);
			}
		});

		searchButton.setText("Search");
		searchButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				searchButtonActionPerformed(evt);
			}
		});
		editButton.setText("Edit");
		editButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				editButtonActionPerformed(evt);
			}

		});
		saveChangeButton.setText("SaveChange");
		saveChangeButton.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				saveChangeButtonActionPerformed(evt);
			}

		});
		stuNoJLabel.setText("学号");

		nameJLabel.setText("姓名");
		addButton.setText("Add");

		deleteButton.setText("Delete");

		genderJLabel.setText("性别");
		birhDateJLabel.setText("出生日期");

		statusJLabel.setText("政治面貌");

		addressJLabel.setText("家庭住址");

		phoneJLabel.setText("电话");

		dormJLabel.setText("宿舍号");

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addGap(58, 58, 58)
										.addComponent(
												stuNoJLabel,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												24,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addGap(18, 18, 18)
										.addComponent(
												stuNoJTextField,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												63,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addComponent(
												nameJLabel,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												32,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addComponent(
																				nameJTextField,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				81,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED,
																				11,
																				Short.MAX_VALUE)
																		.addComponent(
																				genderJLabel)
																		.addGap(13,
																				13,
																				13))
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addComponent(
																				addButton,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				93,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)))
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addComponent(
																				genderJTextField,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				47,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(18,
																				18,
																				18)
																		.addComponent(
																				birhDateJLabel,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				56,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addComponent(
																deleteButton,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																93,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addGap(10,
																				10,
																				10)
																		.addComponent(
																				birthDateJTextField,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				86,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(18,
																				18,
																				18)
																		.addComponent(
																				statusJLabel)
																		.addGap(8,
																				8,
																				8)
																		.addComponent(
																				statusJTextField,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				97,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(18,
																				18,
																				18)
																		.addComponent(
																				addressJLabel,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				53,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																jPanel1Layout
																		.createSequentialGroup()
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				showButton,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				93,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(58,
																				58,
																				58)
																		.addComponent(
																				searchButton,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				93,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(45,
																				45,
																				45)))
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addComponent(
																addressJTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																97,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																editButton,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																93,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(18, 18, 18)
										.addComponent(
												phoneJLabel,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												38,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addGroup(
																jPanel1Layout
																		.createSequentialGroup()
																		.addComponent(
																				phoneJTextField,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				94,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addGap(18,
																				18,
																				18)
																		.addComponent(
																				dormJLabel,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				44,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addComponent(
																				dormJTextField,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				74,
																				javax.swing.GroupLayout.PREFERRED_SIZE))
														.addComponent(
																saveChangeButton,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																104,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(145, Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																nameJLabel)
														.addComponent(
																nameJTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																genderJLabel)
														.addComponent(
																genderJTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																birhDateJLabel)
														.addComponent(
																birthDateJTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																statusJLabel,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																27,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																statusJTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																addressJLabel)
														.addComponent(
																addressJTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																phoneJLabel,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																phoneJTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																dormJLabel)
														.addComponent(
																dormJTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																stuNoJLabel)
														.addComponent(
																stuNoJTextField,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addGap(39, 39, 39)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(addButton)
														.addComponent(
																deleteButton)
														.addComponent(
																showButton)
														.addComponent(
																searchButton)
														.addComponent(
																editButton)
														.addComponent(
																saveChangeButton))
										.addContainerGap(317, Short.MAX_VALUE)));
		getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

		jMenu3.setText("File");

		jMenuItem3.setText("exit");
		jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jMenuItem3ActionPerformed(evt);
			}
		});
		jMenu3.add(jMenuItem3);

		jMenuBar2.add(jMenu3);

		jMenu4.setText("Edit");
		jMenu4.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jMenu4ActionPerformed(evt);
			}
		});

		jMenuItem4.setText("About");
		jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jMenuItem4ActionPerformed(evt);
			}
		});
		jMenu4.add(jMenuItem4);

		jMenuBar2.add(jMenu4);

		setJMenuBar(jMenuBar2);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	protected void saveChangeButtonActionPerformed(ActionEvent evt) {
		// 根据学号唯一性，学号不可修改，据学号找到对应数据库中信息，用文本框中除学号以外的信息进行覆盖
		int rowIndex = jTable1.getSelectedRow();
		if (rowIndex != -1) {
			DefaultTableModel tableModel = (DefaultTableModel) jTable1
					.getModel();
			String stuNo = (String) tableModel.getValueAt(rowIndex, 0);
			StudentDao stuDao = new StudentDaoJTableImpl(jTable1);
			String[] content = new String[8];
			// content[0] = stuNoJTextField.getText();
			content[1] = nameJTextField.getText();
			content[2] = genderJTextField.getText();
			content[3] = birthDateJTextField.getText();
			content[4] = statusJTextField.getText();
			content[5] = addressJTextField.getText();
			content[6] = phoneJTextField.getText();
			content[7] = dormJTextField.getText();
			Student student = new Student(stuNo, content[1], content[2],
					content[3], content[4], content[5], content[6], content[7]);
			boolean flag = stuDao.modifyInTable(rowIndex, content, stuNo);
			stuDao.modifySaveInDB(student, stuNo);
			if (flag && student != null && !nameJTextField.getText().equals("")
					&& !genderJTextField.getText().equals("")
					&& !birthDateJTextField.getText().equals("")
					&& !statusJTextField.getText().equals("")
					&& !addressJTextField.getText().equals("")
					&& !phoneJTextField.getText().equals("")
					&& !dormJTextField.getText().equals("")
					&& stuNoJTextField.getText().equals(stuNo)) {
				JOptionPane.showMessageDialog(null, "修改成功");
			} else {
				JOptionPane.showMessageDialog(null, "修改失败,请检查信息是否有信息为空或变动学号");
			}
		}

	}

	private void showButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_showButtonActionPerformed
		int rowIndex = jTable1.getSelectedRow();
		if (rowIndex != -1) {
			DefaultTableModel tableModel = (DefaultTableModel) jTable1
					.getModel();
			String stuNo = (String) tableModel.getValueAt(rowIndex, 0);
			String stuName = (String) tableModel.getValueAt(rowIndex, 1);
			String gender = (String) tableModel.getValueAt(rowIndex, 2);
			String birthDate = (String) tableModel.getValueAt(rowIndex, 3);
			String politicalStatus = (String) tableModel
					.getValueAt(rowIndex, 4);
			String homeAddress = (String) tableModel.getValueAt(rowIndex, 5);
			String telephone = (String) tableModel.getValueAt(rowIndex, 6);
			String dormitoryNumber = (String) tableModel
					.getValueAt(rowIndex, 7);
			Student student = new Student(stuNo, stuName, gender, birthDate,
					politicalStatus, homeAddress, telephone, dormitoryNumber);
			StringBuilder sb = new StringBuilder();
			sb.append(student.getStuNo() + "\n" + student.getStuName() + "\n"
					+ student.getGender() + "\n" + student.getBirthDate()
					+ "\n" + student.getPoliticalStatus() + "\n"
					+ student.getHomeAddress() + "\n" + student.getTelephone()
					+ "\n" + student.getDormitoryNumber());
			DetailJFrame detailFrame = new DetailJFrame();
			detailFrame.getjTextArea1().append(sb.toString());
			detailFrame.setVisible(true);

		} else {
			JOptionPane.showMessageDialog(null, "未选择");
		}
	}

	private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_searchButtonActionPerformed
		// 调用search界面的main函数
		Search.main();
	}

	private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_addButtonActionPerformed
		StudentDao stuDao = new StudentDaoJTableImpl(jTable1);
		// 判断是否有信息未填写
		if (stuNoJTextField.getText().length() == 0
				|| nameJTextField.getText().length() == 0
				|| genderJTextField.getText().length() == 0
				|| birthDateJTextField.getText().length() == 0
				|| statusJTextField.getText().length() == 0
				|| addressJTextField.getText().length() == 0
				|| phoneJTextField.getText().length() == 0
				|| dormJTextField.getText().length() == 0) {
			JOptionPane.showMessageDialog(null, "您有信息未填写！");
		} else {
			String stuNo = stuNoJTextField.getText();
			if (checkStudentNumExist(stuNo)) {// 判断学号是否已存在
				JOptionPane.showMessageDialog(null, "对不起!该学号已存在，请检查您的输入");
			} else {
				String name = nameJTextField.getText();
				String gender = genderJTextField.getText();
				String birthDate = birthDateJTextField.getText();
				String politicalStatus = statusJTextField.getText();
				String homeAddress = addressJTextField.getText();
				String telephone = phoneJTextField.getText();
				String dormitoryNumber = dormJTextField.getText();
				Student student = new Student(stuNo, name, gender, birthDate,
						politicalStatus, homeAddress, telephone,
						dormitoryNumber);

				boolean flag1 = stuDao.saveInDB(student);// 是否已成功添加到数据库
				boolean flag = stuDao.save(student);// 是否成功添加到表格上
				if (flag && flag1) {
					JOptionPane.showMessageDialog(null, "添加成功");
				} else {
					JOptionPane.showMessageDialog(null, "添加失败!");
				}
			}
		}
	}

	// 判断学号是否已经被注册，若存在则返回true,否则返回false
	private boolean checkStudentNumExist(String number) {
		boolean flag = false;
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		String sql = "select * from student where num like ?";

		try {
			conn = JDBCUtil.getConnection();
			pstat = (PreparedStatement) conn.prepareStatement(sql);
			pstat.setString(1, number);
			rs = pstat.executeQuery();
			while (rs.next()) {
				String stuNo = rs.getString("num");
				if (stuNo.equals(number)) {
					flag = true;
				}
			}

		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.realeaseAll(rs, pstat, conn);
		}
		return flag;
	}

	// 将选中的那一行信息加载到对应文本框中
	private void editButtonActionPerformed(ActionEvent evt) {
		int rowIndex = jTable1.getSelectedRow();
		if (rowIndex != -1) {
			DefaultTableModel tableModel = (DefaultTableModel) jTable1
					.getModel();
			String stuNo = (String) tableModel.getValueAt(rowIndex, 0);
			StudentDao stuDao = new StudentDaoJTableImpl(jTable1);
			Student student = stuDao.modifyFindStudent(stuNo);
			stuNoJTextField.setText(student.getStuNo());
			nameJTextField.setText(student.getStuName());
			genderJTextField.setText(student.getGender());
			birthDateJTextField.setText(student.getBirthDate());
			statusJTextField.setText(student.getPoliticalStatus());
			addressJTextField.setText(student.getHomeAddress());
			phoneJTextField.setText(student.getTelephone());
			dormJTextField.setText(student.getDormitoryNumber());
		} else {
			JOptionPane.showMessageDialog(null, "未选择");
		}
	}

	// 选中一行信息，根据学号将表格及数据库中的对应行删除
	private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {
		int rowIndex = jTable1.getSelectedRow();
		if (rowIndex != -1) {
			DefaultTableModel tableModel = (DefaultTableModel) jTable1
					.getModel();
			String stuNo = (String) tableModel.getValueAt(rowIndex, 0);
			StudentDao stuDao = new StudentDaoJTableImpl(jTable1);
			boolean flag = stuDao.deleteByIndex(rowIndex);
			boolean flag1 = stuDao.deleteStudent(stuNo);
			if (flag & flag1) {
				JOptionPane.showMessageDialog(null, "删除成功");
			} else {
				JOptionPane.showMessageDialog(null, "删除失败!");
			}
		} else {
			JOptionPane.showMessageDialog(null, "未选择");
		}

	}// GEN-LAST:event_deleteButtonActionPerformed

	private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {// GEN-FIRST:event_jTable1MouseClicked

	}// GEN-LAST:event_jTable1MouseClicked

	private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem3ActionPerformed
		System.exit(0);
	}// GEN-LAST:event_jMenuItem3ActionPerformed

	private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jButton5ActionPerformed
		aboutDialog.setVisible(false);
	}// GEN-LAST:event_jButton5ActionPerformed

	private void jMenu4ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenu4ActionPerformed

	}// GEN-LAST:event_jMenu4ActionPerformed

	private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_jMenuItem4ActionPerformed
		aboutDialog.setVisible(true);
		aboutDialog.pack();
	}

	// 在表格上展现所有查询出来的学生信息
	private void displayAllStudent() {
		StudentDao stuDao = new StudentDaoJTableImpl(jTable1);
		DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
		ArrayList<Student> stulist = stuDao.getAllStudent();
		for (Student student : stulist) {
			String[] strs = stuDao.convertStudentToStringArr(student);
			model.addRow(strs);
		}
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main() {

		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}

		/* Create and display the form */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				MainJFrame mainJFrame = new MainJFrame();
				mainJFrame.setVisible(true);

				mainJFrame.displayAllStudent();

			}
		});

	}

	private javax.swing.JButton searchButton;
	private javax.swing.JDialog aboutDialog;
	private javax.swing.JButton addButton;
	private javax.swing.JButton deleteButton;
	private javax.swing.JButton jButton5;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JMenu jMenu3;
	private javax.swing.JMenu jMenu4;
	private javax.swing.JMenuBar jMenuBar2;
	private javax.swing.JMenuItem jMenuItem3;
	private javax.swing.JMenuItem jMenuItem4;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JTable jTable1;
	private javax.swing.JButton showButton;
	private javax.swing.JButton editButton;
	private javax.swing.JButton saveChangeButton;

	private javax.swing.JLabel addressJLabel;
	private javax.swing.JTextField addressJTextField;
	private javax.swing.JLabel birhDateJLabel;
	private javax.swing.JTextField birthDateJTextField;
	private javax.swing.JLabel dormJLabel;
	private javax.swing.JTextField dormJTextField;
	private javax.swing.JLabel genderJLabel;
	private javax.swing.JTextField genderJTextField;
	private javax.swing.JTextField jTextField4;
	private javax.swing.JLabel nameJLabel;
	private javax.swing.JTextField nameJTextField;
	private javax.swing.JLabel phoneJLabel;
	private javax.swing.JTextField phoneJTextField;
	private javax.swing.JLabel statusJLabel;
	private javax.swing.JTextField statusJTextField;
	private javax.swing.JLabel stuNoJLabel;
	private javax.swing.JTextField stuNoJTextField;
}